import prefix from '../str/prefix';

/**
 * 将时间戳转为字符串
 * @param {number} type		转换类型，(0:yyyy-MM-dd,1:yyyy-MM-dd HH:mm:ss,2:yyyy-MM-dd HH:mm)
 */
export default function format(date: number, type: 0 | 1 | 2) {
	const separator = '-';
	const dt = new Date(date);
	const year = dt.getFullYear().toString();
	const month = (dt.getMonth() + 1).toString();
	const day = dt.getDate().toString();
	const ymd = prefix(year, 4) + separator + prefix(month, 2) + separator + prefix(day, 2);
	const hour = dt.getHours().toString();
	const minute = dt.getMinutes().toString();
	switch (type) {
		case 2:
			return `${ymd} ${prefix(hour, 2)}:${prefix(minute, 2)}`;
		case 1:
			const second = dt.getSeconds().toString();
			return `${ymd} ${prefix(hour, 2)}:${prefix(minute, 2)}:${prefix(second, 2)}`;
		default:
			return ymd;
	}
}
